package cn.mifengkong.appcommon.webview;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import cn.mifengkong.appcommon.customecamera.ImageUtils;

/**
 * @author jiangyongxing
 * @date 2018/6/21
 * 描述：
 */

public class BitmapDownloadIntentService extends IntentService {

    private static final String ACTION_INIT_DOWNLOAD_BITMAP = "cn.mifengkong.appcommon.service.action.BitmapDownloadIntentService.ACTION_INIT_DOWNLOAD_BITMAP";

    private static final String BITMAP_URL = "bitmap_url";

    public static void startForSaveBasicInfo(Context context, String... bitmapUrls) {
        Intent intent = new Intent(context, BitmapDownloadIntentService.class);
        intent.putExtra(BITMAP_URL, bitmapUrls);
        intent.setAction(ACTION_INIT_DOWNLOAD_BITMAP);
        context.startService(intent);
    }

    public BitmapDownloadIntentService() {
        super("BitmapDownloadIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }
            switch (action){
                case ACTION_INIT_DOWNLOAD_BITMAP:
                    String[] bitmapUrls = intent.getStringArrayExtra(BITMAP_URL);
                    if (bitmapUrls == null || bitmapUrls.length == 0){
                        return;
                    }
                    ImageUtils.getBitmapForUrlAndSaveToAlbum(getApplicationContext(), bitmapUrls);
                    break;
                    default:
                        break;
            }
        }
    }
}
